define(function(require) {
    'use strict';

    var
        _ = require('underscore'),
        Base = require('components/pop/base'),
        template = require('text!components/pop/templates/goods_url.html'),
        utils = require('core/utils'),
        ZeroClipboard = require('clipboard');


    return Base.extend({
        template: _.template(template),
        className: 'popover left',
        events: {
            'click .js-btn-copy': 'copy'
        },
        initialize: function(options) {
            Base.prototype.initialize.call(this, options);
            ZeroClipboard.config({
                moviePath: window._global.url._static + '/vendor/plugin/ZeroClipboard.swf',
                trustedDomains: ['*'],
                allowScriptAccess: 'always'
            });
            this.data = options.data;
        },
        render: function() {
            var that = this;
            this.$el.html(this.template({url: this.data}));
            var clip = new ZeroClipboard(this.$('.js-btn-copy'));
            var clip2 = new ZeroClipboard(this.$('.js-btn-showsku-copy'));
            clip.on('load', function(client) {
                client.on('complete', function() {
                    utils.successNotify('复制成功');
                    that.hide();
                    clip.off('load');
                });
            });
            clip2.on('load', function(client) {
                client.on('complete', function() {
                    utils.successNotify('复制成功');
                    that.hide();
                    clip2.off('load');
                });
            });
            return this;
        },
        positioning: function() {
            var position = this.target.offset();
            var demension = {
                height: this.target.outerHeight(),
                width: this.target.outerWidth()
            };
            var self = {
                height: this.$el.outerHeight(),
                width: this.$el.outerWidth()
            };

            this.$el.css({
                'display': 'block',
                'left': position.left - self.width,
                'top': position.top - self.height / 2 + demension.height / 2
            });
        },
        setData: function(data) {
            this.data = data;
            this.render();
        },
        reset: function(obj) {
            var callback = obj.callback,
                target = obj.target,
                trigger = obj.trigger,
                data = obj.data;
            this.setCallback(callback);
            this.setTarget(target);
            this.setTrigger(trigger);
            this.setData(data);
            this.positioning();
            this.clearInput();
            this.show();
        },
        copy: function() {
            this.hide();
        },
        clearInput: function() {
            this.$('.js-link-placeholder').val('');
        }
    });
});
